package cn.monkey.state.scheduler.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.function.Supplier;

public class RingBufferConfig {
    private final int bufferSize;
    private final ProducerType producerType;
    private final Supplier<WaitStrategy> waitStrategySupplier;

    private RingBufferConfig(int bufferSize, ProducerType producerType, Supplier<WaitStrategy> waitStrategySupplier) {
        this.bufferSize = bufferSize;
        this.producerType = producerType;
        this.waitStrategySupplier = waitStrategySupplier;
    }

    public int getBufferSize() {
        return bufferSize;
    }

    public ProducerType getProducerType() {
        return producerType;
    }

    public Supplier<WaitStrategy> getWaitStrategySupplier() {
        return waitStrategySupplier;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {
        private int bufferSize = 1024;
        private ProducerType producerType = ProducerType.MULTI;
        private Supplier<WaitStrategy> waitStrategySupplier = BlockingWaitStrategy::new;

        private Builder() {
        }

        public Builder bufferSize(int bufferSize) {
            this.bufferSize = bufferSize;
            return this;
        }

        public Builder producerType(ProducerType producerType) {
            this.producerType = producerType;
            return this;
        }

        public Builder waitStrategySupplier(Supplier<WaitStrategy> waitStrategySupplier) {
            this.waitStrategySupplier = waitStrategySupplier;
            return this;
        }

        public RingBufferConfig build() {
            return new RingBufferConfig(bufferSize, producerType, waitStrategySupplier);
        }
    }
}
